package hello.app.dialog;

import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.widget.EditText;
import android.widget.Toast;

public class CommentBuilder extends Builder {
	EditText mEditText;
	Context mContext;
	onSavedCommentListener mListener;

	public CommentBuilder(Context context, String text) {
		super(context);
		mContext = context;
		mEditText = new EditText(context);
		mEditText.setText(text);
		setIcon(android.R.drawable.btn_star);
		setTitle("コメント");
		setView(mEditText);
		setPositiveButton("OK",new OnPositiveClickListener());
		setNegativeButton("キャンセル",new OnNegativeClickListener());
	}

	public void setOnSavedCommentListener(onSavedCommentListener listener){
		mListener = listener;
	}

	public interface onSavedCommentListener{
		public void onSaved(String text);
	}

	class OnPositiveClickListener implements DialogInterface.OnClickListener{
		public void onClick(DialogInterface dialog, int id) {
			String text = mEditText.getText().toString();
			if(mListener != null && text.length() != 0){
				mListener.onSaved(mEditText.getText().toString());
			}
			if(text.length() == 0){
				Toast.makeText(mContext, "何かコメントを入力して下さい", Toast.LENGTH_SHORT).show();
			}
			dialog.cancel();
		}
	}

	class OnNegativeClickListener implements DialogInterface.OnClickListener{
		public void onClick(DialogInterface dialog, int id) {
			dialog.cancel();
		}
	}

}
